package top.hkyzf.study.designpattern.chain.filter;

import top.hkyzf.study.designpattern.chain.domain.Msg;

import java.util.ArrayList;
import java.util.List;

/**
 * Filter链条
 * @author 朱峰
 * @date 2020-12-16 15:11
 */
public class FilterChain implements Filter{
    List<Filter> filters = new ArrayList<Filter>();
    private int index = 0;
    private int count = 0;

    public FilterChain add(Filter filter) {
        filters.add(filter);
        return this;
    }

    public void doFilter(Msg msg, FilterChain filterChain) {
        if(index == filters.size()) {
            return;
        }
        Filter filter = filters.get(index);
        if(filter instanceof FilterChain) {
            index++;
            filter = ((FilterChain) filter).filters.get(0);
            filter.doFilter(msg, filterChain);
        } else {
            index++;
            filter.doFilter(msg, filterChain);
        }
    }

}
